//
//  HWQQGameController.swift
//  HWanGame
//
//  Created by 王昌健 on 2023/6/2.
//  Copyright © 2023 94hwan. All rights reserved.
//

import UIKit
import JXSegmentedView
class HWQQGameController: HWBaseViewController {
    var navTitle = ""
    var tagButtonBg:UIView!
    var hotButton:UIButton!
    var newButton:UIButton!
    var searchButton:UIButton!
    var historyButton:UIButton!
    
    ///标签
    var segmentedView: JXSegmentedView = JXSegmentedView(frame: CGRect.zero)
    var segmentedDataSource: JXSegmentedTitleDataSource!
    var listContainerView: JXSegmentedListContainerView!
    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        
        initFrame()
        
    }
    
}

extension HWQQGameController {
    private func initUI() -> Void {
        
        self.navigationView.isHidden = false
        self.navigationView.titleLabel.text = "小游戏"
        navigationView.backButton.setImage(UIImage(named: "navBackButton"), for: .normal)
        
        tagButtonBg = UIView()
        tagButtonBg.backgroundColor = .colorWithHexString(hexString: "#EEEEEE")
        tagButtonBg.layer.cornerRadius = 19*widthScale
        tagButtonBg.layer.masksToBounds = true
        view.addSubview(tagButtonBg)
        
        hotButton = UIButton(type: .custom)
        hotButton.setTitle("热游榜", for: .normal)
        hotButton.setTitleColor(.colorWithHexString(hexString: "#F87706"), for: .normal)
        hotButton.titleLabel?.font = .systemFont(ofSize: 18*widthScale)
        hotButton.layer.cornerRadius = 17*widthScale
        hotButton.backgroundColor = .white
        hotButton.addTarget(self, action: #selector(hotButtonClick(_:)), for: .touchUpInside)
        tagButtonBg.addSubview(hotButton)
        
        newButton = UIButton(type: .custom)
        newButton.setTitle("新游榜", for: .normal)
        newButton.setTitleColor(.colorWithHexString(hexString: "#333333"), for: .normal)
        newButton.titleLabel?.font = .systemFont(ofSize: 18*widthScale)
        newButton.layer.cornerRadius = 17*widthScale
        newButton.addTarget(self, action: #selector(newButtonClick(_:)), for: .touchUpInside)
        tagButtonBg.addSubview(newButton)

        segmentedDataSource = JXSegmentedTitleDataSource()
        segmentedDataSource.titles = ["热门榜","新游榜"]
        
        segmentedDataSource.isTitleColorGradientEnabled = true
        segmentedDataSource.titleNormalColor = UIColor.colorWithHexString(hexString: "#555555")
        segmentedDataSource.titleSelectedColor = UIColor.colorWithHexString(hexString: "#FA8C08")
        segmentedDataSource.titleNormalFont = .systemFont(ofSize: 16*widthScale)
        segmentedDataSource.titleSelectedFont = .boldSystemFont(ofSize: 20*widthScale)
        segmentedDataSource.isItemSpacingAverageEnabled = false
        segmentedDataSource.itemSpacing = 18*widthScale
        segmentedView.dataSource = segmentedDataSource
        
        let indicator = JXSegmentedIndicatorImageView()
        indicator.image = UIImage(named: "homeIndicator")
        indicator.indicatorHeight = 7*widthScale
        indicator.indicatorWidth = 31*widthScale
        indicator.contentMode = .scaleAspectFill
        indicator.layer.masksToBounds = true
        indicator.verticalOffset = 1
        segmentedView.indicators = [indicator]
        segmentedView.delegate = self
        segmentedView.backgroundColor = .clear
        view.addSubview(self.segmentedView)
        
        
        listContainerView = JXSegmentedListContainerView(dataSource: self)
        view.addSubview(listContainerView)
        
        segmentedView.listContainer = listContainerView
        
        searchButton = UIButton(type: .custom)
        searchButton.setImage(UIImage(named: "qqGameSearch"), for: .normal)
        searchButton.addTarget(self, action: #selector(searchButtonClick(_:)), for: .touchUpInside)
        self.navigationView.addSubview(searchButton)
        
        historyButton = UIButton(type: .custom)
        historyButton.setImage(UIImage(named: "qqGameHistory"), for: .normal)
        historyButton.addTarget(self, action: #selector(historyButtonClick(_:)), for: .touchUpInside)
        self.navigationView.addSubview(historyButton)
        
    }
    
    private func initFrame() -> Void {
        
        tagButtonBg.snp.makeConstraints { make in
            make.top.equalTo(navigationView.snp.bottom).offset(14*widthScale)
            make.width.equalTo(202*widthScale)
            make.height.equalTo(38*widthScale)
            make.centerX.equalToSuperview()
        }
        
        hotButton.snp.makeConstraints { make in
            make.left.top.equalTo(2*widthScale)
            make.bottom.equalTo(-2*widthScale)
            make.width.equalTo(98*widthScale)
        }
        
        newButton.snp.makeConstraints { make in
            make.top.equalTo(2*widthScale)
            make.right.bottom.equalTo(-2*widthScale)
            make.width.equalTo(98*widthScale)
        }
        
        listContainerView.snp.makeConstraints { make in
            make.left.right.bottom.equalToSuperview()
            make.top.equalTo(tagButtonBg.snp.bottom)
        }
        
        historyButton.snp.makeConstraints { make in
            make.centerY.equalTo(navigationView.titleLabel)
            make.right.equalTo(-15*widthScale)
            make.width.height.equalTo(22*widthScale)
        }
        
        searchButton.snp.makeConstraints { make in
            make.centerY.equalTo(navigationView.titleLabel)
            make.right.equalTo(historyButton.snp.left).offset(-10*widthScale)
            make.width.height.equalTo(22*widthScale)
        }
        
    }
}

extension HWQQGameController:JXSegmentedListContainerViewDataSource,JXSegmentedViewDelegate {
    func numberOfLists(in listContainerView: JXSegmentedListContainerView) -> Int {
        return segmentedDataSource.dataSource.count
    }
    
    func listContainerView(_ listContainerView: JXSegmentedListContainerView, initListAt index: Int) -> JXSegmentedListContainerViewListDelegate {
        let qqGame = HWQQGameTypeController()
        if index == 0 {
            qqGame.type = "0"
        } else {
            qqGame.type = "1"
        }
        
        return qqGame

    }
    
    func segmentedView(_ segmentedView: JXSegmentedView, didSelectedItemAt index: Int) {
        if index == 0 {
            hotButton.setTitleColor(.colorWithHexString(hexString: "#F87706"), for: .normal)
            hotButton.titleLabel?.font = .systemFont(ofSize: 18*widthScale)
            hotButton.layer.cornerRadius = 17*widthScale
            hotButton.backgroundColor = .white
            
            newButton.setTitleColor(.colorWithHexString(hexString: "#333333"), for: .normal)
            newButton.titleLabel?.font = .systemFont(ofSize: 18*widthScale)
            newButton.layer.cornerRadius = 17*widthScale
            newButton.backgroundColor = .clear
        } else {
            newButton.setTitleColor(.colorWithHexString(hexString: "#F87706"), for: .normal)
            newButton.titleLabel?.font = .systemFont(ofSize: 18*widthScale)
            newButton.layer.cornerRadius = 17*widthScale
            newButton.backgroundColor = .white
            
            hotButton.setTitleColor(.colorWithHexString(hexString: "#333333"), for: .normal)
            hotButton.titleLabel?.font = .systemFont(ofSize: 18*widthScale)
            hotButton.layer.cornerRadius = 17*widthScale
            hotButton.backgroundColor = .clear
        }
    }
}

extension HWQQGameController {
    @objc func hotButtonClick(_:UIButton) -> Void {
        hotButton.setTitleColor(.colorWithHexString(hexString: "#F87706"), for: .normal)
        hotButton.titleLabel?.font = .systemFont(ofSize: 18*widthScale)
        hotButton.layer.cornerRadius = 17*widthScale
        hotButton.backgroundColor = .white
        
        newButton.setTitleColor(.colorWithHexString(hexString: "#333333"), for: .normal)
        newButton.titleLabel?.font = .systemFont(ofSize: 18*widthScale)
        newButton.layer.cornerRadius = 17*widthScale
        newButton.backgroundColor = .clear
        segmentedView.selectItemAt(index: 0)
    }
    
    @objc func newButtonClick(_:UIButton) -> Void {
        newButton.setTitleColor(.colorWithHexString(hexString: "#F87706"), for: .normal)
        newButton.titleLabel?.font = .systemFont(ofSize: 18*widthScale)
        newButton.layer.cornerRadius = 17*widthScale
        newButton.backgroundColor = .white
        
        hotButton.setTitleColor(.colorWithHexString(hexString: "#333333"), for: .normal)
        hotButton.titleLabel?.font = .systemFont(ofSize: 18*widthScale)
        hotButton.layer.cornerRadius = 17*widthScale
        hotButton.backgroundColor = .clear
        segmentedView.selectItemAt(index: 1)
    }
    
    @objc func searchButtonClick(_:UIButton) -> Void {
        self.navigationController?.pushViewController(HWQQGameSearchController(), animated: true)
    }
    
    @objc func historyButtonClick(_:UIButton) -> Void {
        self.navigationController?.pushViewController(HWQQGameHistoryController(), animated: true)
    }
}
